package com.ynet.middleground.mobile.approve.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.constant.ErrorMessageConstant;
import com.ynet.middleground.mobile.approve.request.GetSealReq;
import com.ynet.middleground.mobile.approve.request.SendCodeReq;
import com.ynet.middleground.mobile.approve.request.SendLendersCaptchaCodeReq;
import com.ynet.middleground.mobile.approve.response.SealInfoDTO;
import com.ynet.middleground.mobile.approve.service.ApproveCenterService;
import com.ynet.middleground.mobile.approve.service.ContractCenterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName ContractController
 * @Description 合约相关API Controller
 * @Author liwq
 * @Date 2021/3/23 22:02
 * @Version 1.0
 */
@RestController
@RequestMapping("/")
@Api(value = "签章相关服务", tags = "签章相关服务")
public class ContractController {

    @Autowired
    ContractCenterService contractCenterService;

    @Autowired
    ApproveCenterService approveCenterService;

    /**
     * 电子签章-手机号验证码获取
     * 
     * @param sendCodeReq
     * @return
     */
    @PostMapping("/sign/send-code")
    @ApiOperation(value = "获取电子签章手机验证码", notes = "获取电子签章手机验证码")
    public ResponseResult<Object> sendSignCode(@RequestBody @Valid SendCodeReq sendCodeReq) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        ServiceResult<Object> result = null;
        try {
            result = contractCenterService.sendCode(sendCodeReq);
            if (result != null) {
                responseResult.setErrorCode(result.getErrorCode());
                responseResult.setErrorMsg(result.getErrorMsg());
            }
        } catch (BusinessException be) {
            responseResult.setError(be.getCode(), be.getMessage());
            return responseResult;
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "电子签章-手机号验证码获取服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError(ErrorMessageConstant.ECUC0005_CODE, ErrorMessageConstant.ECUC0005_MESSAGE);
        }
        return responseResult;
    }

    /**
     * 获取印模服务
     * 
     * @param req 获取印模信息请求参数
     * @return 用户印模信息
     */
    @PostMapping("/seal")
    @ApiOperation(value = "获取印模信息", notes = "获取印模信息")
    public ResponseResult<SealInfoDTO> getSeal(@RequestBody @Valid GetSealReq req) {
        ResponseResult<SealInfoDTO> result = new ResponseResult<>();
        try {
            result.setData(contractCenterService.getSealById(req.getUserId(), req.getSealId()));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取印模信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 发送放款验证码（通知渠道发送）
     *
     * @param sendCodeReq 发送放款验证码请求信息
     * @return
     */
    @PostMapping("/load/payout/send-code")
    @ApiOperation(value = "发送放款验证码（通知渠道发送）", notes = "发送放款验证码（通知渠道发送")
    public ResponseResult<Object> sendLendersCaptcha(@RequestBody @Valid SendLendersCaptchaCodeReq sendCodeReq) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            approveCenterService.pushCaptchaCodeMsgQueue(sendCodeReq);
        } catch (BusinessException be) {
            responseResult.setError(be.getCode(), be.getMessage());
            return responseResult;
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发送放款验证码（通知渠道发送）服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError(ErrorMessageConstant.ECUC0005_CODE, ErrorMessageConstant.ECUC0005_MESSAGE);
        }
        return responseResult;
    }

}
